﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.ComponentModel.Composition;
using ShredTheLog.Api;
using System.Globalization;

namespace ShredTheLog.ViewModels
{
    public enum OnlineStatus
    {
        Indeterminate,
        Online,
        Offline
    }

    [ValueConversion(typeof(int), typeof(OnlineStatus))]
    public sealed class OnlineStatusConverter : IValueConverter
    {
        [Import]
        private IShredServices ShredServices { get; set; }

        public OnlineStatusConverter()
        {
            Globals.CompositionContainer.SatisfyImportsOnce(this);
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int uid = (int)value;
            var textChatUsers = ShredServices.TextChatUsers;
            if (textChatUsers != null)
                return textChatUsers.Any(i => i.Uid == uid) ? OnlineStatus.Online : OnlineStatus.Offline;
            else
                return OnlineStatus.Indeterminate;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }


        public static readonly OnlineStatusConverter Default = new OnlineStatusConverter();
    }
}
